<?php
namespace app\index\controller;
/**
 * 用户控制器
 * 1、只负责去哪一个页面
 */
use think\Controller;
use lib\ReturnJson;
class User extends Controller{

	function index()
	{
		echo "初始";
		return $this->fetch();

	}

	function test()
	{
		/*	$rj=new ReturnJson;
		$code = "200";
		$message = "asd";
		$data = array(
				"name"=>"1111",
				"sex"=>"1"
			);
		echo $rj ->json($code,$message,$data);
		//接受数据
		$recv = file_get_contents('php://input');
				$obj = json_decode($recv,true); //加true参数生成数组，不加为obj对象
		*/
		$rj=new ReturnJson;
		$code = "200";
		$message = "asd";
		$data = array(
				"name"=>"1111",
				"sex"=>"1"
			);
		echo $rj ->json($code,$message,$data);
	}

	function login()				//登录接口
	{	
	  /*$userModel=M('User');
        $where['username']  = array('eq', $username);
        $where['password']  = array('eq',$password);
        $result= $userModel->where($where)->find();*/
        $rj=new ReturnJson;

        $clientid = input('post.id');
        $clientpwd = input('post.pwd');
        $result = db('user')->where('id',$clientid)->where('pwd',$clientpwd)->find();
        if(empty($result))//如果找不到数据，code=201
        {
            echo $rj->json(201,'用户名或者密码错误',array());
        }
        else
        {
            echo $rj->json(200,'登录成功',$result);
        }

	}

	function reg()				//注册接口
	{
		$rj=new ReturnJson;

		$client = input('post.');
		$clientid = input('post.id');
		$clienticon = input('post.icon');
        $clientpwd = input('post.pwd');

        $result = db('user')->where('id',$clientid)->find();
        //dump($result);
		if(empty($result)){
			db('user')->insert($client);
			echo $rj->json(200,'注册成功','null');
		}else{
			echo $rj->json(201,'注册失败','null');
		}
		
		
	}

	function sendAddress()			//通讯录编辑接口，暂时弃用
	{
		$rj=new ReturnJson;

		$json = request()->post();
		//$json1 = json_encode($json);			//测试用                           应用的时候删除
		$jsondata = json_decode($json,true);//将收到的信息转化为数组格式

		$inputdata = $jsondata['data'];// 取出数组相应元素
		$user1 = $jsondata['user1'];
		$isupdate = $jsondata['isupdate'];

		if(!empty($jsondata)){
			if($isupdate==1){
				db('address')->where('user1',$user1)->delete();
				db('address')->insertAll($inputdata);  //有修改
				echo $rj->json(200,'修改成功','null');
			}else{
				$checkdata = db('address')->where('user1',$user1)->select();
				$res = array();
				foreach($checkdata as $v) $tmp[$v['user2']] = $v;
				foreach($inputdata as $v) if(! isset($tmp[$v['user2']])) $res[] = $v;
				dump($res);
				db('address')->insertAll($res);  //无修改，单一新增
				echo $rj->json(200,'修改成功','null');
			}
		}else{
			echo $rj->json(201,'修改失败','null');
		}
			
	}

	function addAddress()		//增加通讯录接口
	{
		$rj=new ReturnJson;
		$json = request()->post();	
		$jsondata = json_decode($json,true);
		$inputdata = $jsondata['data'];// 取出数组相应元素
		$user1 = $jsondata['user1'];
		$user2 = $jsondata['user2'];

		$result = db('address')->where('user1',$user1)->where('user2',$user2)->find();

		if(empty($result)){
			/*	$insertdata=array();			// 二维降一维处理

			foreach ($inputdata as $key => $value) {
				$insertdata = $value;
			}*/
			$flag = db('address')->insert($inputdata);

			if($flag==1){
				echo $rj->json(200,'增加成功','null');
			}else{
				echo $rj->json(201,'增加失败','null');
			}	
		}else{
			echo $rj->json(202,'关系已存在','null');
		}

		
	}

	function delAddress()		//删除通讯录接口
	{
		$rj=new ReturnJson;
		$json = request()->post();
		$jsondata = json_decode($json,true);
		$user1 = $jsondata['user1'];// 取出数组相应元素
		$user2 = $jsondata['user2'];
		$flag = db('address')->where('user1',$user1)->where('user2',$user2)->delete();
		if($flag==1){
			echo $rj->json(200,'删除成功','null');	
		}else{
			echo $rj->json(201,'删除失败','null');
		}
	}

	function updateAddress()	//修改通讯录接口
	{
		$rj=new ReturnJson;
		$json = request()->post();
		$jsondata = json_decode($json,true);
		$inputdata = $jsondata['data'];
		$user1 = $jsondata['user1'];// 取出数组相应元素
		$user2 = $jsondata['user2'];

		/*	$insertdata=array();			// 二维降一维处理
		foreach ($inputdata as $key => $value) {
			$insertdata = $value;
		}*/
		$flag = db('address')->where('user1',$user1)->where('user2',$user2)->update($inputdata);// 返回影响行数
		if($flag==0){
			echo $rj->json(201,'修改失败','null');	
		}else{
			echo $rj->json(200,'修改成功','null');
		}
	}

	function getAdress()			//获取通讯录接口
	{
		$rj=new ReturnJson;
		$user1 = input('post.user1');

		$checkdata = db('address')->where('user1',$user1)->select();

		if(!empty($checkdata)){
			echo $rj->json(200,'查询成功',$checkdata);
		}else{
			echo $rj->json(201,'查询失败','null');
		}

	}

	function adduser()
	{	
		$data = null;
		for($i=1;$i<5;$i++){
			$temp = ['id'=>$i,'icon'=>'icon','pwd'=>123456];
			$data[$i]=$temp;
		}
		db('user')->insertAll($data);
	}


}


?>